<?php

  /*
   * Copyright 2008 Jeffrey Palm
   */

include 'include.php';

// Connect
$user = config('username');
$pass = config('password');
$email = $user;

// Allow the username to be just that and not an email address But add
// the server name if none is specified.  This works for me but could
// slam someone else's buts in the door, so keep that in mind
if (!preg_match('/.*@.*/',$user)) {
  $server = $_SERVER['SERVER_NAME'];
  $server = preg_replace('/^www\./','',$server);
  $email = $user . '@' . $server;
 }

$mbox = connect($user,$pass);
note("Have mbox: $mbox");

// Options
$numMsgs = imap_num_msg($mbox);
$limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 10;
if ($limit > $numMsgs) {
  $limit = $numMsgs;
 }
$start = $numMsgs;

note("Starting at $start");
note("Have $numMsgs messages");
note("Using limit $limit");

$addresses_to_allow = NULL;
if (config('addresses',FALSE)) {
  $addresses_to_allow = preg_split('/,/',config('addresses'));
 }


// Maybe add a trailing slash to the output directory
$outdir = config('relative_path');
if (!preg_match('/.*\/$/',$outdir)) {
  $outdir .= '/';
 }

// Find the last date
$lines = file('./last.txt');
$lastDate = $lines ? $lines[0] : 0;
note("Have last date '$lastDate'");

note("Writing to output directory '$outdir'");
$count = 0;
$writtenToLastDate = FALSE;
for ($i=0; $count<$limit; $i++) {
  $msgno = $start-$i;
  $header_info = imap_headerinfo($mbox,$msgno);
  
  // Maybe only allow certain addresses
  $keep = TRUE;
  if ($addresses_to_allow) {
    $keep = FALSE;
    foreach ($addresses_to_allow as $addr) {
      if ($from == $addr) {
        $keep = TRUE;
        break;
      }
    }
  }
  if (!$keep) {
    note("Rejecting $from");
    continue;
  }
  
  // Get the contents
  $date = $header_info->date;
  $subj = $header_info->Subject;
  note("Looking at message $date ... '$subj'");

  // Decide whether to quit or not
  if ($lastDate) {
    if ($date == $lastDate) {
      note("all done");
      break;
    }
  }

  $body = imap_fetchbody($mbox,$msgno,"1");
  $img = imap_fetchbody($mbox,$msgno,"2");

  // Write the file to 'photo.jpg'
  $sec = datetime();
  $file           = $outdir . $sec . "-full.jpg";
  $outFile        = $outdir . $sec . ".jpg";
  $outDescription = $outdir . $sec . ".des";
  $outLocation    = $outdir . $sec . ".loc";
  $whoLocation    = $outdir . $sec . ".who";

  $description = $body;
  $location    = $subj;
  $who         = $header_info->fromaddress;

  writeFile($file,imap_base64($img));
  $width  = 160;
  $height = 213;
  doExec("convert -verbose -resize $width" . "x" . "$height $file $outFile");
  writeFile($outDescription, $description);
  writeFile($outLocation,    $location);
  writeFile($whoLocation,    $who);

  // copy latest files
  exec('cp ' . $outFile        . ' ' . $outdir . 'new.jpg');
  exec('cp ' . $outDescription . ' ' . $outdir . 'new.des');
  exec('cp ' . $outFile        . ' ' . $outdir . 'new.loc');

  // write the new.php file
  writeNewBaseFile($description,$location,$sec,$outdir);

  // Write the blog
  writeRSS($outdir);
  
  // Record that we're done
  if (!$writtenToLastDate) {
    writeFile('last.txt',$date);
    $writtenToLastDate = TRUE;
  }

  newline();

  // Success
  $count++;
 }

?>

